/**
 * 
 */
package com.flexengine;

import com.flexengine.config.ConfigurationWindow;
import com.flexengine.engine.FlexEngine;
import com.flexengine.server.FlexEngineServer;
import com.flexengine.server.ServerConsole;
import com.flexengine.logger.Logger;
import com.flexengine.scene.SandBoxScene;
import com.flexengine.scene.SceneManager;

/**
 * @author Luann Athayde
 */
public class FlexEngineBox {
    
    public static void main(String ... args) {
        //Logger.setDebugLevel(Logger.DEBUG_ALL);
        if( args.length > 0 ) {
            for(int i=0;i<args.length;i++) {
                if( args[i].equalsIgnoreCase("--name") ) {
                    if( i+1<args.length ) {
                        FlexEngine.getInstance().setGameName(args[++i]);
                    }
                }
                else if( args[i].equalsIgnoreCase("--setup") ) {
                    Logger.logMessage("Open ConfigurationWindow...");
                    ConfigurationWindow.getInstance().setVisible(true);
                }
                else if( args[i].equalsIgnoreCase("--log") ) {
                    if( i+1<args.length ) {
                        i++;
                        switch(args[i]) {
                            case "all":
                                Logger.setDebugLevel(Logger.DEBUG_ALL);
                                break;
                            case "info":
                                Logger.setDebugLevel(Logger.DEBUG_INFO);
                                break;
                            case "warning":
                                Logger.setDebugLevel(Logger.DEBUG_WARNING);
                                break;
                            case "error":
                                Logger.setDebugLevel(Logger.DEBUG_ERROR);
                                break;
                            default:
                                Logger.setDebugLevel(Logger.DEBUG_INFO);
                                break;
                        }
                    }
                }
                else if( args[i].equalsIgnoreCase("--fps") ) {
                    try {
                        int fps = Integer.valueOf(args[++i]);
                        if( fps>=FlexEngine.UNLIMITED_UPS ) {
                            FlexEngine.getInstance().setDesireUPS(fps);
                        }
                    } catch(Exception e) {}
                }
                else if( args[i].equalsIgnoreCase("--server") ) {
                    String port = "";
                    if( i+1<args.length ) {
                        port   = args[i+1];
                    }
                    String maxCon = "";
                    if( i+2<args.length ) {
                        maxCon = args[i+2];
                    }
                    if( port.startsWith("-p=") ) {
                        try {
                            FlexEngineServer.getInstance().setServerPort(Integer.valueOf(port.substring(3)));
                        } catch(Exception e) {}
                        i++;
                        if( maxCon.startsWith("-m=") ) {
                            try {
                                FlexEngineServer.getInstance().setMaxUsers(Long.valueOf(maxCon.substring(3)));
                            } catch(Exception e) {}
                            i++;
                        }
                    }
                    FlexEngineServer.getInstance().initialize();
                    FlexEngineServer.getInstance().start();
                    ServerConsole.getInstance().waitForCommands();
                    return;
                }
                if( args[i].equalsIgnoreCase("--sandbox") ) {
                    SceneManager.getInstance().setNextScene(new SandBoxScene()).changeScene();
                }
            }
            // -- Check for start basic engine...
            for( String arg : args ) {
                if( arg.equals("--start") ) {
                    FlexEngine.getInstance().start();
                    break;
                }
            }
        } else {
            System.out.println(">-------------------------------> FlexEngineBOX <------------------------------<");
            System.out.println("| Arguments:                                                                   |");
            System.out.println("|    --log [level]   Enable debug log. Level define the level of the log.      |");
            System.out.println("|    --setup         Open the engine's setup.                                  |");
            System.out.println("|    --name          Game name to aply on the engine.                          |");
            System.out.println("|    --fps           Define the fps rate of the engine.                        |");
            System.out.println("|    --start         Start the basic engine.                                   |");
            System.out.println("|                                                                              |");
            System.out.println("|    --server -p=[n] -m=[n]                                                    |");
            System.out.println("|        -p (optional): Define the value of the port to the server.            |");
            System.out.println("|        -m (optional): Define the max numbers of users.                       |");
            System.out.println(">------------------------------------------------------------------------------<");
            System.out.println("Running on: "+FlexEngine.ENGINE_FULL_NAME+" "+FlexEngine.getEnviroment());
        }
    }
    
}
